package com.fan89.controller;


import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.lang.Assert;
import com.fan89.common.lang.Result;
import com.fan89.dto.LinkListDto;
import com.fan89.dto.TypeListDto;
import com.fan89.entity.Tag;
import com.fan89.entity.Type;
import com.fan89.service.TypeService;
import com.fan89.util.ShiroUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.RequiresAuthentication;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author yongbiao.fan
 * @since 2020-07-29
 */
@Api(value = "分类管理api",tags = "分类管理相关接口")
@RestController
public class TypeController {
    @Autowired
    TypeService typeService;

    @ApiOperation("测试奥")
    @GetMapping("/test1")
    public Result test(){
        int i=1/0;
        return Result.success("成功获取全部分类数据",i);
    }

    /**
     * 分类列表
     * @param
     * @return
     */
    @ApiOperation("分类列表")
    @GetMapping("/types")
    public Result list(){

        List<Type> list = typeService.list();
        List<TypeListDto> dtoList=new ArrayList<>(list.size());
        for(Type type:list){
            TypeListDto listDto=new TypeListDto();
            BeanUtil.copyProperties(type,listDto);
            dtoList.add(listDto);
        }
        LinkListDto listDto=new LinkListDto();
        BeanUtil.copyProperties(list,listDto);
        return Result.success("成功获取全部分类数据",dtoList);
    }

    /**
     * 分类详情
     * @param id
     * @return
     */
    @ApiOperation("分类详情")
    @GetMapping("/type/{id}")
    public Result detail(@PathVariable(name = "id")Long id){

        Type type = typeService.getById(id);

        Assert.notNull(type,"本分类已被删除");
        return Result.success("成功读取分类数据",type);
    }

    /**
     * 分类编辑
     * @param
     * @return
     */
    @ApiOperation("分类编辑")
    @RequiresAuthentication //认证后方可访问该接口
    @PostMapping("/type/edit")
    public Result edit(@Validated @RequestBody Tag tag){

        Type temp = null;
        Long currentUserId = ShiroUtils.getProfile().getId().longValue(); //获取当前用户id
        if(tag.getId()!=null){
            temp = typeService.getById(tag.getId());
//            Assert.isTrue(temp.getUserId().longValue()==currentUserId,"无编辑权限");
        }else {
            //初始化新文章
            temp = new Type();
        }
        //复制文章数据
        BeanUtil.copyProperties(tag,temp,"id","create_time");

        typeService.saveOrUpdate(temp);
        return Result.success("成功编辑或创建分类");
    }

    /**
     * 分类删除
     * @param id
     * @return
     */
    @ApiOperation("分类删除")
    @GetMapping("/type/delete/{id}")
    public Result delete(@PathVariable(name = "id")String  id){
//        Long currentUserId = ShiroUtils.getProfile().getId().longValue();
//        if(currentUserId==null&&currentUserId!=1L){
//            return Result.error("抱歉，您没有删除权限");
//        }
        boolean result = typeService.removeById(id);// 据库中为Long id
        Assert.isTrue(result,"该分类已被删除");
        return Result.success("成功删除本分类");
    }
}
